package com.zhp.farmerclient.controller;

import com.zhp.farmerclient.common.Result;
import com.zhp.farmerclient.entity.Event;
import com.zhp.farmerclient.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;

@RestController
@RequestMapping("/event")
@CrossOrigin//解决前端后端跨域问题
public class EventController {

    @Autowired
    public EventService eventService;

    //事件申请
    @PostMapping("/apply")
    public Result apply(@RequestBody Event event){
        return  eventService.apply(event);
    }

    @PostMapping("delete/{id}")
    //撤销申请/删除
    public Result delete(@PathVariable("id") int id){
        return  eventService.delete(id);
    }
    //批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) { // [1,2,3]
        return eventService.deletes(ids);
    }

    //事件审批
    @PostMapping("/accept")
    public Result accept(@RequestBody Event event){
        return eventService.accept(event);
    }

    //根据用户ID查看用户申请列表
    @GetMapping("/id")
    public Result check(@RequestParam Integer page,
                        @RequestParam Integer size,
                        @RequestParam(name = "user_id") Integer id){
        return eventService.check(page,size,id);
    }
    //查看事件详情页
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable(name = "id") int id){
        return eventService.detail(id);
    }


    @Value("${file.upload.path}")
    private String uploadPath;

    //文件上传
    @PostMapping("/upload")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("user_id") Integer userId) {
        // 检查上传的文件类型
        String contentType = file.getContentType();
        if (!contentType.equals("application/pdf") && !contentType.equals("application/msword") && !contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("仅支持pdf、doc、docx格式的文件");
        }
        // 检查上传的文件大小
        long fileSize = file.getSize();
        if (fileSize > 10 * 1024 * 1024) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("文件大小不能超过10MB");
        }
        try {
            // 保存文件
            String fileName = file.getOriginalFilename();
            String filePath = uploadPath + fileName;
            Files.copy(file.getInputStream(), Paths.get(filePath), StandardCopyOption.REPLACE_EXISTING);
            // 返回文件路径
            return ResponseEntity.ok(filePath);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上传失败");
        }
    }

    //分页显示（查询）
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String title,
                           @RequestParam(defaultValue = "") String content,
                           @RequestParam(defaultValue = "") String status) {
        return eventService.page(pageNum,pageSize,title,content,status);
    }

}
